Goでは、 コレクション コレクションは、同じ型のアイテムを効率的にアクセスできるようにグループ化されたものです。"コンテナベイ"は、個別の変数ではなく、単一の識別子で大量のデータを管理する必要性を表しています。
1. 組み込みリテラル
「 組み込みリテラル 」は、任意の複合型を希望する値で初期化するための簡潔な構文です。この構文を使用すると、`type{value1, value2, ...}` のように、一度に配列の宣言と初期化を行うことができます。 type{value1, value2, ...}。
2. ゼロベースのインデックス
配列は0からインデックスが開始されます。8つの惑星からなるコレクションは、インデックス0~7でアクセスします。この範囲外のインデックスにアクセスすると、コンパイル時エラーまたはランタイムパンクが発生します。
図16.1:インデックス0~7の惑星
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary purpose of a composite literal in Go?
To perform complex mathematical calculations on groups of numbers.
A concise syntax to declare and initialize a composite type with values.
To convert a slice into an array automatically.
To delete elements from a collection.
✅ Correct!
Correct! It allows you to define the structure and the data in one step.❌ Incorrect
Composite literals are for initialization, not calculation or deletion.QUESTION 2
If an array has 8 elements (as in Figure 16.1), what is the index of the first item?
1
-1
0
8
✅ Correct!
Correct. Go uses zero-based indexing for all collections.❌ Incorrect
Collections in Go always start at index 0.QUESTION 3
What does the ellipsis (...) in a function declaration typically indicate?
The function is incomplete.
The function accepts a variable number of arguments (variadic).
The function will loop infinitely.
The function returns a pointer.
✅ Correct!
Yes, the ellipsis denotes variadic parameters.❌ Incorrect
The ellipsis is used for variadic functions, allowing them to take any number of values.QUESTION 4
In the context of the Go Cargo Bay, what are 'collections'?
Variables that can only store numbers.
Just groups of things, typically of the same type.
Special functions that sort data.
Hardware components used for storage.
✅ Correct!
Exactly. Arrays, slices, and maps are all types of collections.❌ Incorrect
Collections are logical groupings of data within the code.QUESTION 5
What happens if you attempt to access planets[8] in an array of size 8?
It returns the first element again (wrap around).
It returns a null or empty string.
It causes a compile-time error or a runtime panic.
It automatically expands the array to size 9.
✅ Correct!
Correct! Array sizes are fixed and bounds are strictly enforced.❌ Incorrect
Arrays in Go are fixed-length; you cannot access an index equal to or greater than the length.Module Challenge: The Ship's Manifest
Representing physical items as digital arrays.
You are organizing a spaceship's inventory. You have a collection of personal items like stamps, books, and trophies. You need to initialize these in Go and perform operations on them.
Q
1. Arrays are for collecting many of the same type of thing. What collections from your own life (e.g., stamps, shoes) could you represent with an array? Explain your choice.
Solution:
Answers will vary. For example: 'A collection of 10 pairs of shoes could be represented as [10]string because shoes are of the same type (footwear) and the count is fixed in my closet.'
Answers will vary. For example: 'A collection of 10 pairs of shoes could be represented as [10]string because shoes are of the same type (footwear) and the count is fixed in my closet.'
Q
2. Provide the syntax for a 'Step' function signature that takes two Universe types, intended for simulation operations.
Solution:
The signature is:
The signature is:
func Step(a, b Universe). This allows the function to operate on two collections, often used for double-buffering in simulations like the Game of Life.